package com.keyancompany.zhongbaopingtai.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;

import com.photoselector.ui.PhotoSelectorActivity;

import java.io.File;
import java.io.Serializable;
import java.util.Iterator;

public class IntentUtils {

    public static final int TAKEPICTURE_CODE = 100;
    public static File file;

    private IntentUtils() {
    }

    public static void startActivity(Context context, Class target) {
        Intent intent = new Intent(context, target);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, Class target, String key,
                                     String value) {
        Intent intent = new Intent(context, target);
        intent.putExtra(key, value);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, Class target, String key,
                                     String value, int flag) {
        Intent intent = new Intent(context, target);
        intent.putExtra(key, value);
        intent.setFlags(flag);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, Class target, String key,
                                     boolean value) {
        Intent intent = new Intent(context, target);
        intent.putExtra(key, value);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, Class target, String key,
                                     Serializable value) {
        Intent intent = new Intent(context, target);
        intent.putExtra(key, value);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, Class target, String key,
                                     Integer value) {
        Intent intent = new Intent(context, target);
        intent.putExtra(key, value);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, Class target,
                                     Bundle bundle) {
        Intent intent = new Intent(context, target);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    public static void startActivity(Context context, Class target, String key,
                                     Bundle bundle) {
        Intent intent = new Intent(context, target);
        intent.putExtra(key, bundle);
        context.startActivity(intent);
    }

    public static void startBorwser(Context context, Uri uri) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(uri);
        context.startActivity(intent);
    }

    public static void startActivityWithPhotoForResult(Activity context, Class target,
                                                       int length, int requestCode) {
        Intent intent = new Intent(context, PhotoSelectorActivity.class);
        intent.putExtra("maxLength", length);
        context.startActivityForResult(intent, requestCode);
    }

    public static void startActivityForResult(Activity context, Class<?> cls,
                                              int requestCode) {
        Intent intent = new Intent(context, cls);
        context.startActivityForResult(intent, requestCode);
    }

    public static void startActivityForResult(Context context,
                                              Class<?> cls, String key, Bundle value, int requestCode) {
//        ComponentName componentName = new ComponentName(
//                context.getPackageName(), classname);
        Intent intent = new Intent(context, cls);
        if (value != null) {
            intent.putExtra(key, value);
        }
//        intent.setComponent(componentName);
        ((Activity) context).startActivityForResult(intent, requestCode);
    }

    // public static void takePicture(Activity context) {
    // try {
    // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // file = new File(FileUtils.getExternalCacheDir(context,
    // CacheManager.DIR), String.valueOf(
    // System.currentTimeMillis()).concat(".jpg"));
    // intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    // context.startActivityForResult(intent, RequestCode.TAKE_PICTURE);
    // } catch (Exception e) {
    // Toast.makeText(context,
    // context.getString(R.string.start_camera_fail),
    // Toast.LENGTH_LONG).show();
    // }
    // }
    //
    // public static void pickImage(Activity context) {
    // Intent picture = new Intent(Intent.ACTION_PICK,
    // android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // context.startActivityForResult(picture, RequestCode.PICK_IMAGE);
    // }

    public static void callPhone(Activity context, String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel://" + phone));
        context.startActivity(intent);
    }

    public static void sendToSony(Context context, String number) {
        boolean isShow = true;
        if ("0".equals(number)) {
            isShow = false;
        }
        Intent localIntent = new Intent();
        localIntent
                .putExtra(
                        "com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE",
                        isShow);// 是否显示
        localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        localIntent.putExtra(
                "com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",
                getLaunchActivityName(context));// 启动页
        localIntent.putExtra(
                "com.sonyericsson.home.intent.extra.badge.MESSAGE", number);// 数字
        localIntent.putExtra(
                "com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME",
                context.getPackageName());// 包名
        context.sendBroadcast(localIntent);
    }

    public static void sendToSamsumg(Context context, String number) {
        Intent localIntent = new Intent(
                "android.intent.action.BADGE_COUNT_UPDATE");
        localIntent.putExtra("badge_count", number);// 数字
        localIntent.putExtra("badge_count_package_name",
                context.getPackageName());// 包名
        localIntent.putExtra("badge_count_class_name",
                getLaunchActivityName(context)); // 启动页
        context.sendBroadcast(localIntent);
    }

    public static String getLaunchActivityName(Context context)

    {

        PackageManager localPackageManager = context.getPackageManager();

        Intent localIntent = new Intent("android.intent.action.MAIN");

        localIntent.addCategory("android.intent.category.LAUNCHER");

        try

        {

            Iterator<ResolveInfo> localIterator = localPackageManager
                    .queryIntentActivities(localIntent, 0).iterator();

            while (localIterator.hasNext())

            {

                ResolveInfo localResolveInfo = localIterator.next();

                if (!localResolveInfo.activityInfo.applicationInfo.packageName
                        .equalsIgnoreCase(context.getPackageName()))

                    continue;

                String str = localResolveInfo.activityInfo.name;

                return str;

            }

        } catch (Exception localException)

        {
            localException.printStackTrace();
        }

        return null;
    }

    public static void playVideo(Context context, String path) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(path), "video/*");
        context.startActivity(intent);

    }

    public static boolean currentTopActivity(Context context, String packagename) {
        if (context != null) {
            ActivityManager manager = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            RunningTaskInfo info = manager.getRunningTasks(1).get(0);
            String shortClassName = info.topActivity.getPackageName();
            if (packagename == null) {
                return false;
            }
            if (shortClassName == null) {
                return false;
            }
            return packagename.equals(shortClassName);
        }
        return false;
    }

}
